<template>
    <div class="contract">
        <ContentWrap class="left">
            <div class="card-header">
                <button @click="cancell">返回列表</button>
            </div>
            <div class="main">
                <h3 class="title"></h3>
                <div class="address">
                    <Icon class="mr-5px" icon="ep:location" />
                    <!-- 太原市小店区长风街98号 -->
                    <!-- <button @click="getCurrentLocation(AMap)">定位</button> -->
                </div>  
                <div class="imgurll">
                    <UploadImgs v-model="queryParams.pics"  :multiple="true"/>
                </div>
                <div class="main-tit">
                    <div class="main-content">
                        <div class="main-hez">
                            <div class="main-left">
                                <el-form :model="queryParams" label-width="80px">
                                    <el-form-item label="项目名称">
                                        <el-input v-model="queryParams.name" style="width: 333px;" />
                                    </el-form-item>
                                    <el-form-item label="总包公司"  class="formBox">
                                       <el-select placeholder="请选择总包公司" v-model="queryParams.company" clearable>
                                            <el-option v-for="(item) in userList" :key="item.id" :label="item.nickname" :value="item.nickname" />
                                        </el-select>
                                        <!-- <el-input v-model="queryParams.company" style="width: 333px;" /> -->
                                    </el-form-item>
                                    <el-form-item label="地址    ">
                                        <el-input v-model="queryParams.address" style="width: 333px;" />
                                    </el-form-item>
                                    <el-form-item 
                                        class="formBox" label="开始时间">
                                        <el-date-picker
                                        v-model="queryParams.ontime"
                                        type="date"
                                        placeholder="选择开始时间">
                                        </el-date-picker>
                                        <!-- <el-input v-model="queryParams.ontime" style="width: 333px;" /> -->
                                    </el-form-item>
                                    <el-form-item   class="formBox" label="结束时间">
                                        <el-date-picker
                                        v-model="queryParams.creatime"
                                        type="date"
                                        placeholder="选择结束时间">
                                        </el-date-picker>
                                        <!-- <el-input v-model="queryParams.creatime" style="width: 333px;" /> -->
                                    </el-form-item>
                                    <el-form-item label="经度">
                                        <el-input v-model="queryParams.lat" style="width: 333px;" />
                                    </el-form-item>
                                    <el-form-item label="纬度">
                                        <el-input v-model="queryParams.lon" style="width: 333px;" />
                                    </el-form-item>
                                    <el-form-item label="省">
                                        <el-input v-model="queryParams.province" style="width: 333px;" />
                                    </el-form-item>
                                    <el-form-item label="市">
                                        <el-input v-model="queryParams.city" style="width: 333px;" />
                                    </el-form-item>
                                    <el-form-item label="区">
                                        <el-input v-model="queryParams.area" style="width: 333px;" />
                                    </el-form-item>
                                    <el-form-item label="项目经理">
                                        <el-select v-model="queryParams.userId" placeholder="请选择项目经理">
                                            <el-option label="123" value="123" />
                                        </el-select>
                                    </el-form-item>
                                    <el-form-item label="财务    ">
                                        <el-select v-model="queryParams.financeId" placeholder="请选择财务">
                                            <el-option label="123" value="123" />
                                        </el-select>
                                    </el-form-item>
                                    <el-form-item label="订单数量">
                                        <el-input v-model="queryParams.orderNumber" type="number"/>
                                    </el-form-item>
                                    <el-form-item>
                                        <el-button @click="cancell">取消</el-button>
                                        <el-button type="primary" @click="getList">保存并网签</el-button>
                                    </el-form-item>
                                </el-form>

                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </ContentWrap>
        <ContentWrap class="right">
            <iframe id="geoPage" width=0 height=0 frameborder=0  style="display:none;" scrolling="no"
                src="https://apis.map.qq.com/tools/geolocation?key=XAXBZ-2BCK5-2B3IR-QV75W-2SDAV-7QFNX&referer=hhh">
            </iframe>

            <div id="map" class="map" v-show="!isMapLoadingDown"></div>
            <!-- <div class="map"  v-show="isMapLoadingDown">地图加载中...</div> -->
            <el-main v-loading="true" element-loading-text="地图加载中"  v-show="isMapLoadingDown" class="elmain"></el-main>
        </ContentWrap>
    </div>
</template>

<script setup lang="ts">
import { createProject } from "@/api/system/project/index"
import { useRouter } from "vue-router";
import * as UserApi from '@/api/system/user'
const userList = ref([])
let isMapLoadingDown = ref(true)
onBeforeMount(async() => {
    window._bd_share_main = ''; 
})
onMounted(async() => {
    // 加载用户列表
    userList.value = await UserApi.getSimpleUserAllList()
    window.addEventListener('message', function(event) {
        // 接收位置信息 你的其他操作
        var loc = event.data;
        console.log('location', loc);
        if(loc && loc.lat && loc.lng){
           fixedPos(loc)
        }

    }, false);
})
// 点击定位-定位到当前位置
const  fixedPos =(loc)=> {
    console.log(loc,'定位成功')
    queryParams.lat = loc.lat
    queryParams.lon = loc.lng
    isMapLoadingDown.value = false
    var map = document.getElementById("map")
    var pos = new qq.maps.LatLng(loc.lat,loc.lng)
    
    var mapInfo = new qq.maps.Map(map, {
        // 地图的中心地理坐标。
        center: pos,
        zoom:20
    });
// var marker = new qq.maps.Marker({
    //     position: new qq.maps.LatLng(loc.lat,loc.lng), // 标记的经纬度
    //     map, // 地图实例
    // });
     //创建一个Marker
     var marker = new qq.maps.Marker({
        //设置Marker的位置坐标
        position: pos,
        //设置显示Marker的地图
        map: mapInfo
    });
    marker.setVisible(true);
    //设置Marker的动画属性为从落下
    marker.setAnimation(qq.maps.MarkerAnimation.DOWN);
    //设置Marker是否可以被拖拽，为true时可拖拽，false时不可拖拽，默认属性为false
    marker.setDraggable(true);
     //添加信息窗口
     var info = new qq.maps.InfoWindow({
        map: mapInfo
    });
    info.setPosition(marker.getPosition());
    //标记Marker点击事件
    qq.maps.event.addListener(marker, 'click', function() {
        info.setPosition(marker.getPosition());
    });
    //设置Marker停止拖动事件
    qq.maps.event.addListener(marker, 'dragend', function() {
        // info.open();
        console.log(marker.getPosition(),'11111155555')
        let pos = marker.getPosition()
        queryParams.lat = pos.lat
        queryParams.lon = pos.lng
        info.setPosition(marker.getPosition());
    });
   
}
const router = useRouter();
// 表单
const queryParams: any = reactive({
    pics:[]
})
const loading = ref(true) // 列表的加载中
// 取消
const cancell = () => {
    router.push('/system/project/project')
}

/** 确定 */
const getList = async () => {
    loading.value = true;
    let stime = Date.parse(queryParams.ontime)
    let etime = Date.parse(queryParams.creatime)
    // return
    let res = await createProject({
        id: 2,
        name: queryParams.name,
        pics: queryParams.pics.join(','),
        company: queryParams.company,
        province: queryParams.province,
        city: queryParams.city,
        area: queryParams.area,
        address: queryParams.address,
        lat: queryParams.lat,
        userId: queryParams.userId,
        financeId: queryParams.financeId,
        orderNumber: queryParams.orderNumber,
        lon: queryParams.lon,
        startTime: stime,
        endTime: etime
    })
    console.log(res)
    router.push('/system/project/project')
}


</script>


<style lang="scss" scoped>
.elmain{
    width: 100%;
   height: 800px;
}
.contract {
    display: flex;
    width: 100%;
    height: 100%;

    .drop {
        width: 8px;
        height: 8px;
        border: 2px solid rgba(81, 123, 255, 1);
    }

    .left {
        border-radius: 20px;
        margin-right: 20px;
        height: 100%;

        .card-header {
            span {
                font-size: 12px;
            }
        }

        .main {
            .title {
                font-size: 22px;
                font-weight: 500;
                margin: 24px 0px 7px 0px;
            }

            .main-tit {
                .main-content {
                    margin-bottom: 7px;

                    .main-hez {
                        display: flex;
                        border-radius: 16px;
                        background: rgba(249, 250, 255, 1);
                        padding: 24px;

                        .shoping {
                            display: flex;
                            justify-content: space-between;
                            width: 100%;

                            .el-progress {
                                width: 40%;
                            }
                        }

                        .main-left {
                            p {
                                font-size: 16px;
                                font-weight: 500;
                                letter-spacing: 0px;
                                line-height: 23.17px;
                                color: rgba(0, 0, 0, 0.6);
                                margin-bottom: 18px;

                                span {
                                    color: rgba(0, 0, 0, 1);
                                }
                            }
                        }

                        .main-right {
                            p {
                                margin: 10px 0px;
                            }
                        }
                    }
                }
            }
        }
    }

    .address {
        font-size: 14px;
        font-weight: 500;
        letter-spacing: 0px;
        line-height: 20.27px;
        color: rgba(0, 0, 0, 0.4);
        margin-bottom: 20px;
    }

    .imgurll {
        display: flex;

        ::v-deep .el-upload {
            width: 100px;
            height: 74px;
            border-radius: 10px;
        }
        ::v-deep .upload-handle{
            width: 100px;
            height: 74px;
            border-radius: 10px;
         }
        ::v-deep .upload-image{
            width: 100px;
            height: 74px;
            border-radius: 10px;
         }
        ::v-deep  .el-upload-list__item{
             width: 100px;
            height: 74px;
            border-radius: 10px;
         }
        .imgs {
            margin-right: 22px;
        }
    }

    .right {
        flex: 1;
        border-radius: 20px;

        #map {
            width: 100%;
            height: 1000px;
            border-radius: 20px;
        }
    }
}

::v-deep.el-form-item--default .el-form-item__label {
    width: 80px;
    justify-content: flex-start
}

::v-deep.el-select .el-input__wrapper {
    width: 337px;
}

::v-deep.el-form-item--default .el-form-item__content {
    // justify-content: flex-end;
}

::v-deep .amap-logo {
    opacity: 0 !important;
}

::v-deep .amap-copyright {
    opacity: 0 !important;
}
.formBox ::v-deep .el-input__wrapper{
    width: 333px !important;
}

</style>